﻿using LJ.Abstractions.Extensions;
using LJ.Abstractions.Pack;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LJ.Host.Abstractions.Pack.Internal;

public class PackServerManager: IPackServerManager
{
    private readonly IDependencyManager _dependencyManager;
    private readonly IReflectionManager _reflectionManager;


    public PackServerManager(IDependencyManager dependencyManager, IReflectionManager reflectionManager)
    {
        _dependencyManager = dependencyManager;
        _reflectionManager = reflectionManager;

        var types = FindTypes();


        foreach (var type in types)
        {
            _dependencyManager.ServiceCollection.AddSingleton(typeof(IPackServer),type);
        }
    }
    
    private List<Type> FindTypes()
    {
        Type[] baseTypes = [typeof(IPackServer)];
        return _reflectionManager.GetTypes(type => type.IsClass && !type.IsAbstract && !type.IsInterface
            && baseTypes.Any(b => b.IsAssignableFrom(type)) );
    }
}
